package com.ergou.easylive.admin.controller.common;

import com.ergou.easylive.admin.annotation.AccessLog;
import com.ergou.easylive.common.entity.response.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试控制器
 * 用于演示访问日志切面的功能
 *
 * @author Er-Gou
 * @date 2025-10-04
 */
@RestController
@RequestMapping("/test")
@Tag(name = "测试接口", description = "用于测试访问日志切面功能")
@Slf4j
public class TestController {

    /**
     * 测试接口1 - 使用@AccessLog注解
     */
    @GetMapping("/hello")
    @Operation(summary = "测试问候接口", description = "返回问候信息")
    @AccessLog(value = "测试问候接口", includeArgs = true, includeResult = true)
    public Result<String> hello(@RequestParam(defaultValue = "World") String name) {
        log.info("执行hello方法，参数：{}", name);
        return Result.success("Hello, " + name + "!");
    }

    /**
     * 测试接口2 - 不使用@AccessLog注解（会被切面自动拦截）
     */
    @GetMapping("/info")
    @Operation(summary = "获取系统信息", description = "返回系统基本信息")
    public Result<String> getInfo() {
        log.info("执行getInfo方法");
        return Result.success("系统运行正常");
    }

    /**
     * 测试接口3 - 带多个参数的接口
     */
    @GetMapping("/calculate")
    @Operation(summary = "计算接口", description = "执行简单计算")
    @AccessLog(value = "计算接口", includeArgs = true, includeResult = true)
    public Result<Integer> calculate(@RequestParam int a, @RequestParam int b, @RequestParam String operation) {
        log.info("执行calculate方法，参数：a={}, b={}, operation={}", a, b, operation);
        
        int result;
        switch (operation.toLowerCase()) {
            case "add":
                result = a + b;
                break;
            case "subtract":
                result = a - b;
                break;
            case "multiply":
                result = a * b;
                break;
            case "divide":
                if (b == 0) {
                    return Result.fail("除数不能为零");
                }
                result = a / b;
                break;
            default:
                return Result.fail("不支持的操作：" + operation);
        }
        
        return Result.success(result);
    }
}
